<template>
  <div class="app-container">
    <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="85px"
    >
      <el-form-item label="类别" prop="category">
        <el-input
            v-model="queryParams.category"
            placeholder="请输入类别"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="居住地" prop="address">
        <el-input
            v-model="queryParams.address"
            placeholder="请输入居住地"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号码" prop="cardID">
        <el-input
            v-model="queryParams.cardID"
            placeholder="请输入身份证号码"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
            v-model="queryParams.age"
            placeholder="请输入年龄"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学段" prop="schoolLevel">
        <el-input
            v-model="queryParams.schoolLevel"
            placeholder="请输入学段"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校名称" prop="schoolName">
        <el-input
            v-model="queryParams.schoolName"
            placeholder="请输入学校名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校编号" prop="schoolID">
        <el-input
            v-model="queryParams.schoolID"
            placeholder="请输入学校编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-input
            v-model="queryParams.grade"
            placeholder="请输入年级"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级" prop="sclass">
        <el-input
            v-model="queryParams.sclass"
            placeholder="请输入班级"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学号" prop="sno">
        <el-input
            v-model="queryParams.sno"
            placeholder="请输入学号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="公众号ID" prop="accountId">
              <el-input
                  v-model="queryParams.accountId"
                  placeholder="请输入公众号ID"
                  clearable
                  @keyup.enter="handleQuery"
              />
            </el-form-item>
            <el-form-item label="创建时间" prop="createdTime">
              <el-date-picker
                  clearable
                  v-model="queryParams.createdTime"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择创建时间"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="更新时间" prop="updatedTime">
              <el-date-picker
                  clearable
                  v-model="queryParams.updatedTime"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择更新时间"
              >
              </el-date-picker>
            </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:checkperson:add']"
        >新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:checkperson:edit']"
        >修改
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:checkperson:remove']"
        >删除
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:checkperson:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkpersonList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="类别" align="center" prop="category"/>
      <el-table-column label="居住地" align="center" prop="address"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="身份证号码" align="center" prop="cardID"/>
      <el-table-column label="性别" align="center" prop="sex"/>
      <el-table-column label="年龄" align="center" prop="age"/>
      <el-table-column label="学段" align="center" prop="schoolLevel"/>
      <el-table-column label="学校名称" align="center" prop="schoolName"/>
      <el-table-column label="学校编号" align="center" prop="schoolID"/>
      <el-table-column label="年级" align="center" prop="grade"/>
      <el-table-column label="班级" align="center" prop="sclass"/>
      <el-table-column label="学号" align="center" prop="sno"/>
      <el-table-column label="公众号ID" align="center" prop="accountId"/>
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:checkperson:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:checkperson:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改参与筛查人员信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
          ref="checkpersonRef"
          :model="form"
          :rules="rules"
          label-width="80px"
      >
        <el-form-item label="类别" prop="category">
          <el-input v-model="form.category" placeholder="请输入类别"/>
        </el-form-item>
        <el-form-item label="居住地" prop="address">
          <el-input v-model="form.address" placeholder="请输入居住地"/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="身份证号码" prop="cardID">
          <el-input v-model="form.cardID" placeholder="请输入身份证号码"/>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄"/>
        </el-form-item>
        <el-form-item label="学段" prop="schoolLevel">
          <el-input v-model="form.schoolLevel" placeholder="请输入学段"/>
        </el-form-item>
        <el-form-item label="学校名称" prop="schoolName">
          <el-input v-model="form.schoolName" placeholder="请输入学校名称"/>
        </el-form-item>
        <el-form-item label="学校编号" prop="schoolID">
          <el-input v-model="form.schoolID" placeholder="请输入学校编号"/>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入年级"/>
        </el-form-item>
        <el-form-item label="班级" prop="sclass">
          <el-input v-model="form.sclass" placeholder="请输入班级"/>
        </el-form-item>
        <el-form-item label="学号" prop="sno">
          <el-input v-model="form.sno" placeholder="请输入学号"/>
        </el-form-item>
        <el-form-item label="公众号ID" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入公众号ID"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker
              clearable
              v-model="form.createdTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择创建时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedTime">
          <el-date-picker
              clearable
              v-model="form.updatedTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择更新时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Checkperson">
import {
  listCheckperson,
  getCheckperson,
  delCheckperson,
  addCheckperson,
  updateCheckperson,
} from "@/api/system/checkperson";

const {proxy} = getCurrentInstance();

const checkpersonList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    category: null,
    address: null,
    name: null,
    cardID: null,
    sex: null,
    age: null,
    schoolLevel: null,
    schoolName: null,
    schoolID: null,
    grade: null,
    sclass: null,
    sno: null,
    accountId: null,
    createdTime: null,
    updatedTime: null,
  },
  rules: {
    category: [{required: true, message: "类别不能为空", trigger: "blur"}],
    address: [{required: true, message: "居住地不能为空", trigger: "blur"}],
    name: [{required: true, message: "姓名不能为空", trigger: "blur"}],
    cardID: [
      {required: true, message: "身份证号码不能为空", trigger: "blur"},
    ],
    sex: [{required: true, message: "性别不能为空", trigger: "change"}],
    accountId: [
      {required: true, message: "公众号ID不能为空", trigger: "blur"},
    ],
  },
});

const {queryParams, form, rules} = toRefs(data);

/** 查询参与筛查人员信息列表 */
function getList() {
  loading.value = true;
  listCheckperson(queryParams.value).then((response) => {
    checkpersonList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    category: null,
    address: null,
    name: null,
    cardID: null,
    sex: null,
    age: null,
    schoolLevel: null,
    schoolName: null,
    schoolID: null,
    grade: null,
    sclass: null,
    sno: null,
    accountId: null,
    createdTime: null,
    updatedTime: null,
  };
  proxy.resetForm("checkpersonRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加参与筛查人员信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getCheckperson(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改参与筛查人员信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["checkpersonRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateCheckperson(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCheckperson(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
      .confirm('是否确认删除参与筛查人员信息编号为"' + _ids + '"的数据项？')
      .then(function () {
        return delCheckperson(_ids);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {
      });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
      "system/checkperson/export",
      {
        ...queryParams.value,
      },
      `checkperson_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
